package com.ruoyi.management.mock.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.management.basics.vo.GetBannerListVo;
import com.ruoyi.management.constant.NoticeStatusEnum;
import com.ruoyi.management.helper.NoticeServiceHelper;
import com.ruoyi.management.mock.domain.TNotice;
import com.ruoyi.management.mock.mapper.TNoticeMapper;
import com.ruoyi.management.mock.ro.*;
import com.ruoyi.management.mock.service.ITNoticeService;
import com.ruoyi.management.mock.vo.GetNoticeByIdVo;
import com.ruoyi.management.mock.vo.GetNoticeListVo;
import com.ruoyi.passenger.notice.ro.GetDriverHnoticeListParam;
import com.ruoyi.passenger.notice.ro.GetHnoticeListParam;
import com.ruoyi.passenger.notice.ro.GetHnoticeParam;
import com.ruoyi.passenger.notice.vo.GetNoticeVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 公告Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-29
 */
@Service
public class TNoticeServiceImpl implements ITNoticeService {
    @Resource
    private TNoticeMapper tNoticeMapper;

    @Value("${file.preUrl}")
    private String preUrl;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public GetNoticeByIdVo selectTNoticeById(GetNoticeByIdParam param) {
        //获取db对象
        GetNoticeByIdVo vo = tNoticeMapper.selectTNoticeById(param);
        if (vo == null) {
            throw new ServiceException("该公告不存在，请核对数据再点击查看！");
        }
        if (null != vo && null!= vo.getBannerUrl()&& !"".equals(vo.getBannerUrl()))
        {
            vo.setBannerUrl(preUrl+vo.getBannerUrl());
        }
        return vo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GetNoticeListVo> selectTNoticeList(GetNoticeListParam param) {
        List<GetNoticeListVo> voList = tNoticeMapper.selectTNoticeList(param);
        if (CollUtil.isEmpty(voList)) {
            return new ArrayList<>();
        }
        return voList;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addTNotice(AddNoticeParam param) {

        if(!StringUtils.isEmpty(param.getIsTop()) && param.getIsTop().equals("Y")){
            Long topId = tNoticeMapper.selectTop();
            if(topId != null){
                TNotice tNotice = new TNotice();
                tNotice.setId(topId);
                tNotice.setIsTop("N");
                int update = tNoticeMapper.updateNoticeById(tNotice);
                if (update == 0) {
                    throw new ServiceException("新增公告失败，请稍后再试。");
                }
            }
        }
        int insert = tNoticeMapper.addNotice(param);
        if (insert == 0) {
            throw new ServiceException("新增公告失败，请稍后再试");
        }
        return insert;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTNotice(UpdateNoticeParam param) {
        String bannerUrl = param.getBannerUrl();
        if(!StringUtils.isEmpty(bannerUrl) && bannerUrl.contains(preUrl)){
            bannerUrl = bannerUrl.substring(38,bannerUrl.length());
            param.setBannerUrl(bannerUrl);
        }
        //获取到公告表信息,编辑信息
        TNotice main = tNoticeMapper.getNoticeById(param.getId());
        if (main == null) {
            throw new ServiceException("暂无数据。");
        }
        if (Objects.equals(NoticeStatusEnum.ON_SHELVES.getCode(), main.getStatus())){
            throw new ServiceException("该公告已上架，不可编辑!");
        }
        if(!StringUtils.isEmpty(param.getIsTop()) && param.getIsTop().equals("Y")){
            Long topId = tNoticeMapper.selectTop();
            if(topId != null){
                TNotice tNotice = new TNotice();
                tNotice.setId(topId);
                tNotice.setIsTop("N");
                int update = tNoticeMapper.updateNoticeById(tNotice);
                if (update == 0) {
                    throw new ServiceException("更新公告失败，请稍后再试。");
                }
            }
        }
        //2.更新到公告信息
        int update = tNoticeMapper.updateNotice(param);
        if (update == 0) {
            throw new ServiceException("更新公告失败，请稍后再试。");
        }
        return update;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int noticeShelves(NoticeShelvesParam param) {
        //获取到公告表信息,编辑信息
        TNotice main = tNoticeMapper.getNoticeById(param.getId());
        if (main == null) {
            throw new ServiceException("暂无数据。");
        }
        main.setStatus(param.getStatus());
        if (Objects.equals(NoticeStatusEnum.ON_SHELVES.getCode(), param.getStatus())) {
            //修改发布时间
            main.setReleaseTime(DateUtils.getNowDate());
        }else {
            //修改下架时间
            main.setDownTime(DateUtils.getNowDate());
        }
        int update = tNoticeMapper.updateStatus(main);
        if (update == 0) {
            throw new ServiceException("公告"+ (param.getStatus().equals("0") ? "上架" : "下架") + "失败,请稍候再试");
        }
        return update;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GetBannerListVo> getBannerList() {
        List<GetBannerListVo> list = tNoticeMapper.getBannerList();
        for (GetBannerListVo vo : list)
        {
            if (null != vo && null!= vo.getBannerUrl()&& !"".equals(vo.getBannerUrl()))
            {
                vo.setBannerUrl(preUrl+vo.getBannerUrl());
            }
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GetBannerListVo> selectBannerList() {
        List<GetBannerListVo> list = tNoticeMapper.selectBannerList();
        for (GetBannerListVo vo : list)
        {
            if (null != vo && null!= vo.getBannerUrl()&& !"".equals(vo.getBannerUrl()))
            {
                vo.setBannerUrl(preUrl+vo.getBannerUrl());
            }
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GetNoticeByIdVo> getNoticeList(GetDriverHnoticeListParam param) {
        List<GetNoticeByIdVo> list = tNoticeMapper.getNoticeList(param);if (null !=  list)
        {
            for (GetNoticeByIdVo vo : list)
            {
                if (null != vo && null!= vo.getBannerUrl()&& !"".equals(vo.getBannerUrl()))
                {
                    vo.setBannerUrl(preUrl+vo.getBannerUrl());
                }
            }
        }

        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GetNoticeByIdVo> selectNoticeList(GetHnoticeListParam param) {
        List<GetNoticeByIdVo> list = tNoticeMapper.selectNoticeList();
        if (null !=  list)
        {
            for (GetNoticeByIdVo vo : list)
            {
                if (null != vo && null!= vo.getBannerUrl()&& !"".equals(vo.getBannerUrl()))
                {
                    vo.setBannerUrl(preUrl+vo.getBannerUrl());
                }
            }
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int downNotice() {
        return tNoticeMapper.downNotice();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GetNoticeVO selectNoticeById(GetHnoticeParam param) {
        return tNoticeMapper.selectNoticeById(param.getNoticeId());
    }
}
